﻿using IceCream.icemodel;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Reflection.Emit;
using System.Threading;
using System.Windows;

namespace IceCream
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public static int IceInit = 9;
        public static int GSInit = 1;
        public static int GJInit = 1;
        public static int NCInit = 5;
        public static int PCJInit = 3;
        /// <summary>
        /// 空闲
        /// </summary>
        public static bool IsMake = true;
        /// <summary>
        /// 冰淇淋出餐
        /// </summary>
        public static bool IsCSMake = true;
        /// <summary>
        /// 奶茶出餐
        /// </summary>
        public static bool IsMilkMake = true;
        /// <summary>
        /// 冰淇淋出餐完成
        /// </summary>
        public static bool IsCSMakeOR = false;
        /// <summary>
        /// 奶茶出餐完成
        /// </summary>
        public static bool IsMilkMakeOR = false;
        /// <summary>
        /// 奶茶杯子
        /// </summary>
        public static bool IsCupNC = true;
        /// <summary>
        /// 冰淇淋杯子
        /// </summary>
        public static bool IsCupICE = true;
        /// <summary>
        /// 冰淇淋逻辑
        /// </summary>
        public static bool IsIceLC = false;
        /// <summary>
        /// 奶茶逻辑
        /// </summary>
        public static bool IsMilkLC = false;
        public static bool IsMsgError = true;
        public static int MakingType = 0;
        public static string MakingName = "";
        public static int MakingCode = 0;
        public static string IPorPortUrl = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"\IPorPortMessageice.txt";
        public static string coffeeMakeListsUrl = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"\coffeeMakeLists.txt";
        public MainWindow()
        {
            InitializeComponent();
        }
        public static string EmailError()
        {
            try
            {
                string msg = "<style>table{ border: 1px solid black; border-collapse: collapse; }table td{border: 1px solid #000000; padding: 5px 15px;font-size:14px;}</style><table>";
                if (DiaitalBool.Instance.CoffeeBean1_Sel_Bol)
                    msg += "<tr><td style='font-weight:bold;'>CoffeeBeanLeft</td><td>adequate materials</td></tr>";
                else
                    msg += "<tr><td style='font-weight:bold;'>CoffeeBeanLeft</td><td style='color:#FF0000;'>Lack of material</td></tr>";
                if (DiaitalBool.Instance.CoffeeBean2_Sel_Bol)
                    msg += "<tr><td style='font-weight:bold;'>CoffeeBeanRight</td><td>adequate materials</td></tr>";
                else
                    msg += "<tr><td style='font-weight:bold;'>CoffeeBeanRight</td><td style='color:#FF0000;'>Lack of material</td></tr>";
                if (DiaitalBool.Instance.Milk1_Sel_Bol)
                    msg += "<tr><td style='font-weight:bold;'>Milk1</td><td>adequate materials</td></tr>";
                else
                    msg += "<tr><td style='font-weight:bold;'>Milk1</td><td style='color:#FF0000;'>Lack of material</td></tr>";
                if (DiaitalBool.Instance.Milk2_Sel_Bol)
                    msg += "<tr><td style='font-weight:bold;'>Milk2</td><td>adequate materials</td></tr>";
                else
                    msg += "<tr><td style='font-weight:bold;'>Milk2</td><td style='color:#FF0000;'>Lack of material</td></tr>";
                if (DiaitalBool.Instance.CupFeed1_Sel_Bol)
                    msg += "<tr><td style='font-weight:bold;'>Cup1</td><td>adequate materials</td></tr>";
                else
                    msg += "<tr><td style='font-weight:bold;'>Cup1</td><td style='color:#FF0000;'>Lack of material</td></tr>";
                if (DiaitalBool.Instance.CupFeed2_Sel_Bol)
                    msg += "<tr><td style='font-weight:bold;'>Cup2</td><td>adequate materials</td></tr>";
                else
                    msg += "<tr><td style='font-weight:bold;'>Cup2</td><td style='color:#FF0000;'>Lack of material</td></tr>";
                if (DiaitalBool.Instance.Water_Sel_Bol)
                    msg += "<tr><td style='font-weight:bold;'>Water</td><td>adequate materials</td></tr>";
                else
                    msg += "<tr><td style='font-weight:bold;'>Water</td><td style='color:#FF0000;'>Lack of material</td></tr>";
                if (!IsMsgError)
                    msg += "<tr><td style='font-weight:bold;'>Coffee Maker</td><td>Ready</td></tr>";
                else
                    msg += "<tr><td style='font-weight:bold;'>Coffee Maker</td><td style='color:#FF0000;'>NotReady</td></tr>";
                msg += "</table>";
                return msg;
            }
            catch (Exception)
            {
                return "";
            }
        }
        public static string EmailYBJ()
        {
            try
            {
                string msg = "<style>table{ border: 1px solid black; border-collapse: collapse; }table td{border: 1px solid #000000; padding: 5px 15px;font-size:14px;}</style><table>";
                if (DiaitalBool.Instance.WaterYBJ_Sel_Bol)
                    msg += "<tr><td style='font-weight:bold;'>Water</td><td>adequate materials</td></tr>";
                else
                    msg += "<tr><td style='font-weight:bold;'>Water</td><td style='color:#FF0000;'>Pre-alarm materials</td></tr>";
                if (DiaitalBool.Instance.Milk1YBJ_Sel_Bol)
                    msg += "<tr><td style='font-weight:bold;'>Milk1</td><td>adequate materials</td></tr>";
                else
                    msg += "<tr><td style='font-weight:bold;'>Milk1</td><td style='color:#FF0000;'>Pre-alarm materials</td></tr>";
                if (DiaitalBool.Instance.Milk2YBJ_Sel_Bol)
                    msg += "<tr><td style='font-weight:bold;'>Milk2</td><td>adequate materials</td></tr>";
                else
                    msg += "<tr><td style='font-weight:bold;'>Milk2</td><td style='color:#FF0000;'>Pre-alarm materials</td></tr>";
                msg += "</table>";
                return msg;
            }
            catch (Exception)
            {
                return "";
            }
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (!File.Exists(IPorPortUrl))
                {
                    using (FileStream stream = File.Create(IPorPortUrl))
                    {
                        stream.Close();
                        stream.Dispose();
                        IPorPortInfo porPortInfo1 = IPorPortInfo.Instance;
                        File.WriteAllText(IPorPortUrl, JsonConvert.SerializeObject(porPortInfo1));
                    }
                }
                else
                {
                    var dd = File.ReadAllText(IPorPortUrl);
                    var porPortInfo = JsonConvert.DeserializeObject<IPorPortInfo>(File.ReadAllText(IPorPortUrl));
                    IPorPortInfo.Instance.CoffeeIP = porPortInfo.CoffeeIP;
                    IPorPortInfo.Instance.CoffeePort = porPortInfo.CoffeePort;
                    IPorPortInfo.Instance.RobotIPLeft = porPortInfo.RobotIPLeft;
                    IPorPortInfo.Instance.IOPortName = porPortInfo.IOPortName;
                    IPorPortInfo.Instance.ElsePortName = porPortInfo.ElsePortName;
                    IPorPortInfo.Instance.GJInit = porPortInfo.GJInit;
                    IPorPortInfo.Instance.GSInit = porPortInfo.GSInit;
                    IPorPortInfo.Instance.NCInit = porPortInfo.NCInit;
                    IPorPortInfo.Instance.PCJInit = porPortInfo.PCJInit;
                    IPorPortInfo.Instance.IceInit = porPortInfo.IceInit;
                }
                MainWindow.GJInit = IPorPortInfo.Instance.GJInit;
                MainWindow.GSInit = IPorPortInfo.Instance.GSInit;
                MainWindow.NCInit = IPorPortInfo.Instance.NCInit;
                MainWindow.PCJInit = IPorPortInfo.Instance.PCJInit;
                MainWindow.IceInit = IPorPortInfo.Instance.IceInit;
                Thread thr = new Thread(SocketClients.Instance.socketclient);
                thr.IsBackground = true;
                thr.Start();

                Thread ICthr = new Thread(IceMsg.Instance.IOSerial_Thr);
                ICthr.IsBackground = true;
                ICthr.Start();

            }
            catch (Exception)
            {
                return;
            }
        }
        public static void MessageShow()
        {
            try
            {
                while (true)
                {
                    try
                    {
                        Device device = new Device();
                        string lists = JsonConvert.SerializeObject(device);
                        var data = HttpMsg.Instance.deviceOrderQueueListMsg(lists);

                        if (data != null)
                        {
                            var msglist = JsonConvert.DeserializeObject<HTTPMsg>(data);
                            if (msglist != null)
                            {
                                if (msglist.data.ToString().Length > 10)
                                {
                                    var list = JsonConvert.DeserializeObject<List<deviceOrderQueueList>>(msglist.data.ToString());
                                    if (list.Count > 0)
                                    {
                                        var ddd = list.FirstOrDefault(a => a.typeList3.Count > 0);
                                        var ddd1 = list.FirstOrDefault(a => a.typeList1.Count > 0);
                                        if (ddd == null && ddd1 == null) continue;
                                        //if (list[0].typeList3 == null && list[0].typeList1 == null) continue;
                                        if (ddd != null)
                                        {
                                            if (ddd.typeList3.Count > 0)
                                            {
                                                var make = ddd.typeList3.FirstOrDefault(a => a.status == "3" || a.status == "4");
                                                if (make != null)
                                                {
                                                    RobotOperate.CX_Start();
                                                    Console.WriteLine(DateTime.Now.ToString() + "  cx5");
                                                    if (RobotOperate.pstatus == JKTYPE.ProgramState.PROGRAM_RUNNING)
                                                    {
                                                        IceMsg.Instance.AllBol = true;
                                                        ICSerialPort.Instance.SerialSend(IceMsgbool.Instance.IceType);
                                                        Thread.Sleep(200);
                                                        if (IsCupICE && IsCSMake && IceMsg.Instance.isBql)
                                                        {
                                                            IOMsg.Instance.AllBol = false;
                                                            IOMsg.Instance.RobotBol = false;
                                                            Thread.Sleep(200);
                                                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_DYD);
                                                            Thread.Sleep(500);
                                                            if (!IOMsg.Instance.RobotBol)
                                                            {
                                                                Thread.Sleep(200);
                                                                IOMsg.Instance.AllBol = true;
                                                                continue;
                                                            }
                                                            Thread.Sleep(200);
                                                            IOMsg.Instance.AllBol = true;
                                                            IceMsg.Instance.AllBol = false;
                                                            MakingShow(make);
                                                        }
                                                        else
                                                        {
                                                            if (ddd1.typeList1.Count > 0)
                                                            {
                                                                var make1 = ddd1.typeList1.FirstOrDefault(a => a.status == "3" || a.status == "4");
                                                                if (make1 != null)
                                                                {

                                                                    RobotOperate.CX_Start();
                                                                    Console.WriteLine(DateTime.Now.ToString() + "  cx5");
                                                                    if (RobotOperate.pstatus == JKTYPE.ProgramState.PROGRAM_RUNNING)
                                                                    {
                                                                        if (IsCupNC && IsMilkMake)
                                                                        {
                                                                            IOMsg.Instance.AllBol = false;
                                                                            IOMsg.Instance.RobotBol = false;
                                                                            Thread.Sleep(200);
                                                                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_DYD);
                                                                            Thread.Sleep(500);
                                                                            if (!IOMsg.Instance.RobotBol)
                                                                            {
                                                                                Thread.Sleep(200);
                                                                                IOMsg.Instance.AllBol = true;
                                                                                continue;
                                                                            }
                                                                            Thread.Sleep(200);
                                                                            IOMsg.Instance.AllBol = true;
                                                                            MakingMilkShow(make1);
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                                else if (ddd1.typeList1.Count > 0)
                                                {
                                                    var make1 = ddd1.typeList1.FirstOrDefault(a => a.status == "3" || a.status == "4");
                                                    if (make1 != null)
                                                    {
                                                        RobotOperate.CX_Start();
                                                        Console.WriteLine(DateTime.Now.ToString() + "  cx5");
                                                        if (RobotOperate.pstatus == JKTYPE.ProgramState.PROGRAM_RUNNING)
                                                        {
                                                            if (IsCupNC && IsMilkMake)
                                                            {
                                                                IOMsg.Instance.AllBol = false;
                                                                IOMsg.Instance.RobotBol = false;
                                                                Thread.Sleep(200);
                                                                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_DYD);
                                                                Thread.Sleep(500);
                                                                if (!IOMsg.Instance.RobotBol)
                                                                {
                                                                    Thread.Sleep(200);
                                                                    IOMsg.Instance.AllBol = true;
                                                                    continue;
                                                                }
                                                                Thread.Sleep(200);
                                                                IOMsg.Instance.AllBol = true;
                                                                MakingMilkShow(make1);
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                        else if (ddd1.typeList1.Count > 0)
                                        {
                                            var make = ddd1.typeList1.FirstOrDefault(a => a.status == "3" || a.status == "4");
                                            if (make != null)
                                            {
                                                RobotOperate.CX_Start();
                                                Console.WriteLine(DateTime.Now.ToString() + "  cx5");
                                                if (RobotOperate.pstatus == JKTYPE.ProgramState.PROGRAM_RUNNING)
                                                {
                                                    if (IsCupNC && IsMilkMake)
                                                    {
                                                        IOMsg.Instance.AllBol = false;
                                                        IOMsg.Instance.RobotBol = false;
                                                        Thread.Sleep(200);
                                                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_DYD);
                                                        Thread.Sleep(500);
                                                        if (!IOMsg.Instance.RobotBol)
                                                        {
                                                            Thread.Sleep(200);
                                                            IOMsg.Instance.AllBol = true;
                                                            continue;
                                                        }
                                                        Thread.Sleep(200);
                                                        IOMsg.Instance.AllBol = true;
                                                        MakingMilkShow(make);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                                else if (MakingCode == 8)
                                {

                                    if (IsCupICE && IsCSMake && IceMsg.Instance.isBql)
                                    {
                                        IOMsg.Instance.AllBol = false;
                                        IOMsg.Instance.RobotBol = false;
                                        Thread.Sleep(200);
                                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_DYD);
                                        Thread.Sleep(500);
                                        if (!IOMsg.Instance.RobotBol)
                                        {
                                            Thread.Sleep(200);
                                            IOMsg.Instance.AllBol = true;
                                            continue;
                                        }
                                        Thread.Sleep(200);
                                        IOMsg.Instance.AllBol = true;
                                        MakingMilkShows(MakingType.ToString());
                                        MakingCode = 0;
                                    }

                                }
                                else if (MakingCode == 7)
                                {

                                    if (IsCupNC && IsMilkMake)
                                    {
                                        IOMsg.Instance.AllBol = false;
                                        IOMsg.Instance.RobotBol = false;
                                        Thread.Sleep(200);
                                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_DYD);
                                        Thread.Sleep(500);
                                        if (!IOMsg.Instance.RobotBol)
                                        {
                                            Thread.Sleep(200);
                                            IOMsg.Instance.AllBol = true;
                                            continue;
                                        }
                                        Thread.Sleep(200);
                                        IOMsg.Instance.AllBol = true;
                                        MakingShows(MakingName, MakingType.ToString());
                                    }
                                }
                            }
                        }
                        Thread.Sleep(1000);
                    }
                    catch (Exception)
                    {
                        //return;
                        Thread.Sleep(1000);
                    }
                }
            }
            catch
            {
                return;
            }
        }
        /// <summary>
        /// 制作奶茶
        /// </summary>
        /// <param name="list"></param>
        public static void MakingMilkShows(string classcode)
        {
            try
            {

                IsCSMake = false;

                IOMsg.Instance.AllBol = false;
                IOMsg.Instance.RobotBol = false;
                Thread.Sleep(200);
                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_NC_UP);
                while (true)
                {
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_DDNC);
                    Thread.Sleep(100);
                    if (IOMsg.Instance.RobotBol)
                    {
                        break;
                    }
                    Thread.Sleep(100);
                }
                Thread.Sleep(100);
                IsMilkLC = true;
                if (classcode == "2001")
                {
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.NC1_start);
                    Thread.Sleep(IPorPortInfo.Instance.NCInit * 1000);
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.NC1_end);
                }
                else if (classcode == "2002")
                {
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.NC2_start);
                    Thread.Sleep(IPorPortInfo.Instance.NCInit * 1000);
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.NC2_end);
                }
                else if (classcode == "2003")
                {
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.NC3_start);
                    Thread.Sleep(IPorPortInfo.Instance.NCInit * 1000);
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.NC3_end);
                }
                else if (classcode == "2004")
                {
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.NC4_start);
                    Thread.Sleep(IPorPortInfo.Instance.NCInit * 1000);
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.NC4_end);
                }
                else if (classcode == "2005")
                {
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.NC5_start);
                    Thread.Sleep(IPorPortInfo.Instance.NCInit * 1000);
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.NC5_end);
                }
                else if (classcode == "2006")
                {
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.NC6_start);
                    Thread.Sleep(IPorPortInfo.Instance.NCInit * 1000);
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.NC6_end);
                }

                Thread.Sleep(200);
                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_NCWC_UP);
                Thread.Sleep(200);
                IOMsg.Instance.AllBol = false;
                IOMsg.Instance.RobotBol = false;
                Thread.Sleep(200);
                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_NC_Down);
                IsCupNC = false;
                IsMilkMakeOR = true;
                while (true)
                {
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_JFWCNC);
                    Thread.Sleep(200);
                    if (IOMsg.Instance.RobotBol)
                    {
                        break;
                    }
                    Thread.Sleep(100);
                }
                Thread.Sleep(200);
                IOMsg.Instance.AllBol = true;
                Clear();
                IsCSMake = true;
            }
            catch (Exception)
            {
                IsCSMake = true;
                return;
            }
        }
        /// <summary>
        /// 制作奶茶
        /// </summary>
        /// <param name="list"></param>
        public static void MakingMilkShow(DeviceOrderQueue list)
        {
            try
            {

                IsCSMake = false;
                var msgs = JsonConvert.DeserializeObject<List<specification>>(list.jsonCodeVal);
                var classcode = msgs.FirstOrDefault(a => a.classCode != "")?.classCode;
                EditDeviceOrderStatus status = new EditDeviceOrderStatus();
                status.orderGoodsId = list.id;
                status.orderId = list.orderId;
                status.status = "4";
                string lists = JsonConvert.SerializeObject(status);
                while (true)
                {
                    var statuslist = HttpMsg.Instance.editDeviceOrderStatusMsg(lists);
                    if (statuslist != null)
                    {
                        if (statuslist != "")
                        {
                            break;
                        }
                    }
                    Thread.Sleep(100);
                }
                IOMsg.Instance.AllBol = false;
                IOMsg.Instance.RobotBol = false;
                Thread.Sleep(200);
                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_NC_UP);
                while (true)
                {
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_DDNC);
                    Thread.Sleep(100);
                    if (IOMsg.Instance.RobotBol)
                    {
                        break;
                    }
                    Thread.Sleep(100);
                }
                Thread.Sleep(100);
                IsMilkLC = true;
                if (classcode == "2001")
                {
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.NC1_start);
                    Thread.Sleep(IPorPortInfo.Instance.NCInit * 1000);
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.NC1_end);
                }
                else if (classcode == "2002")
                {
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.NC2_start);
                    Thread.Sleep(IPorPortInfo.Instance.NCInit * 1000);
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.NC2_end);
                }
                else if (classcode == "2003")
                {
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.NC3_start);
                    Thread.Sleep(IPorPortInfo.Instance.NCInit * 1000);
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.NC3_end);
                }
                else if (classcode == "2004")
                {
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.NC4_start);
                    Thread.Sleep(IPorPortInfo.Instance.NCInit * 1000);
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.NC4_end);
                }
                else if (classcode == "2005")
                {
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.NC5_start);
                    Thread.Sleep(IPorPortInfo.Instance.NCInit * 1000);
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.NC5_end);
                }
                else if (classcode == "2006")
                {
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.NC6_start);
                    Thread.Sleep(IPorPortInfo.Instance.NCInit * 1000);
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.NC6_end);
                }

                Thread.Sleep(200);
                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_NCWC_UP);
                Thread.Sleep(200);
                IOMsg.Instance.AllBol = false;
                IOMsg.Instance.RobotBol = false;
                Thread.Sleep(200);
                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_NC_Down);
                IsCupNC = false;
                IsMilkMakeOR = true;
                while (true)
                {
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_JFWCNC);
                    Thread.Sleep(200);
                    if (IOMsg.Instance.RobotBol)
                    {
                        break;
                    }
                    Thread.Sleep(100);
                }
                Thread.Sleep(200);
                IOMsg.Instance.AllBol = true;
                Thread.Sleep(100);
                Clear();
                EditDeviceOrderStatus status2 = new EditDeviceOrderStatus();
                status2.orderGoodsId = list.id;
                status2.orderId = list.orderId;
                status2.status = "5";
                string lists2 = JsonConvert.SerializeObject(status2);
                while (true)
                {
                    var statuslist2 = HttpMsg.Instance.editDeviceOrderStatusMsg(lists2);
                    if (statuslist2 != null)
                    {
                        if (statuslist2 != "")
                        {
                            break;
                        }
                    }
                    Thread.Sleep(100);
                }
                IsCSMake = true;
            }
            catch (Exception)
            {
                IsCSMake = true;
                return;
            }
        }

        /// <summary>
        /// 制作冰淇淋
        /// </summary>
        /// <param name="list"></param>
        public static void MakingShow(DeviceOrderQueue list)
        {
            try
            {

                IsCSMake = false;
                string sauceCode = "";
                string nutletCode = "";
                var msgs = JsonConvert.DeserializeObject<List<specification>>(list.jsonCodeVal);
                if (msgs != null)
                {
                    sauceCode = msgs.Any(a => a.GJCode == "1") ? "1" : msgs.Any(a => a.GJCode == "2") ? "2" : msgs.Any(a => a.GJCode == "3") ? "3" : msgs.Any(a => a.GJCode == "4") ? "4" : msgs.Any(a => a.GJCode == "5") ? "5" : msgs.Any(a => a.GJCode == "6") ? "6" : msgs.Any(a => a.GJCode == "7") ? "7" : "";
                    nutletCode = msgs.Any(a => a.XLCode == "1") ? "1" : msgs.Any(a => a.XLCode == "2") ? "2" : msgs.Any(a => a.XLCode == "3") ? "3" : msgs.Any(a => a.XLCode == "4") ? "4" : msgs.Any(a => a.XLCode == "5") ? "5" : msgs.Any(a => a.XLCode == "6") ? "6" : msgs.Any(a => a.XLCode == "7") ? "7" : "";
                }
                EditDeviceOrderStatus status = new EditDeviceOrderStatus();
                status.orderGoodsId = list.id;
                status.orderId = list.orderId;
                status.status = "4";
                string lists = JsonConvert.SerializeObject(status);
                while (true)
                {
                    var statuslist = HttpMsg.Instance.editDeviceOrderStatusMsg(lists);
                    if (statuslist != null)
                    {
                        if (statuslist != "")
                        {
                            break;
                        }
                    }
                    Thread.Sleep(100);
                }
                IOMsg.Instance.AllBol = false;
                IOMsg.Instance.RobotBol = false;
                Thread.Sleep(500);
                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_JBQL_UP);
                if (sauceCode != "")
                {
                    Thread.Sleep(200);
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_JGJ_UP);
                }
                if (nutletCode != "")
                {
                    Thread.Sleep(200);
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_JGS_UP);
                }
                while (true)
                {
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_DDBQL);
                    Thread.Sleep(1000);
                    if (IOMsg.Instance.RobotBol)
                    {
                        break;
                    }
                    Thread.Sleep(1000);
                }
                IsIceLC = true;
                Thread.Sleep(100);
                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_JBQL_Down);
                Thread.Sleep(100);
                //IOMsg.Instance.AllBol = true;
                // IceMsg.Instance.AllBol = false;
                Thread.Sleep(200);
                ICSerialPort.Instance.SerialSend(IceMsgbool.Instance.IceChuLiaoStart);
                Thread.Sleep(200);
                while (true)
                {
                    ICSerialPort.Instance.SerialSend(IceMsgbool.Instance.IceChuLiaoOver);
                    Thread.Sleep(200);
                    if (IceMsg.Instance.isover)
                    {
                        break;
                    }
                    Thread.Sleep(100);
                }
                IOMsg.Instance.AllBol = false;
                Thread.Sleep(200);
                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_JBQLWC_UP);
                Thread.Sleep(200);
                //  IOMsg.Instance.AllBol = true;
                //  IceMsg.Instance.AllBol = true;
                if (sauceCode != "")
                {
                    IOMsg.Instance.AllBol = false;
                    IOMsg.Instance.RobotBol = false;
                    Thread.Sleep(200);
                    while (true)
                    {
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_DDGJ);
                        Thread.Sleep(200);
                        if (IOMsg.Instance.RobotBol)
                        {
                            break;
                        }
                        Thread.Sleep(100);
                    }
                    Thread.Sleep(100);
                    if (sauceCode == "1")
                    {
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.GJ1_start);
                        Thread.Sleep(IPorPortInfo.Instance.GJInit * 1000);
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.GJ1_end);
                    }
                    else if (sauceCode == "2")
                    {
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.GJ2_start);
                        Thread.Sleep(IPorPortInfo.Instance.GJInit * 1000);
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.GJ2_end);
                    }
                    else if (sauceCode == "3")
                    {
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.GJ3_start);
                        Thread.Sleep(IPorPortInfo.Instance.GJInit * 1000);
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.GJ3_end);
                    }
                    Thread.Sleep(200);
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_JGJWC_UP);
                    Thread.Sleep(200);
                    // IOMsg.Instance.AllBol = true;
                }
                if (nutletCode != "")
                {
                    IOMsg.Instance.AllBol = false;
                    IOMsg.Instance.RobotBol = false;
                    Thread.Sleep(200);
                    while (true)
                    {
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_DDGS);
                        Thread.Sleep(200);
                        if (IOMsg.Instance.RobotBol)
                        {
                            break;
                        }
                        Thread.Sleep(100);
                    }
                    Thread.Sleep(100);
                    if (nutletCode == "1")
                    {
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.GS_start);
                        Thread.Sleep(IPorPortInfo.Instance.GSInit * 1000);
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.GS_end);
                    }
                    else if (nutletCode == "2")
                    {
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.GR_start);
                        Thread.Sleep(IPorPortInfo.Instance.GSInit * 1000);
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.GR_end);
                    }
                    Thread.Sleep(200);
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_JGSWC_UP);
                    Thread.Sleep(200);
                    //  IOMsg.Instance.AllBol = true;
                }
                IOMsg.Instance.AllBol = false;
                IOMsg.Instance.RobotBol = false;
                Thread.Sleep(200);
                IsCupICE = false;
                IsCSMakeOR = true;
                while (true)
                {
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_JFWC);
                    Thread.Sleep(200);
                    if (IOMsg.Instance.RobotBol)
                    {
                        break;
                    }
                    Thread.Sleep(100);
                }
                Thread.Sleep(200);
                IceMsg.Instance.AllBol = true;
                Clear();
                EditDeviceOrderStatus status2 = new EditDeviceOrderStatus();
                status2.orderGoodsId = list.id;
                status2.orderId = list.orderId;
                status2.status = "5";
                string lists2 = JsonConvert.SerializeObject(status2);
                while (true)
                {
                    var statuslist2 = HttpMsg.Instance.editDeviceOrderStatusMsg(lists2);
                    if (statuslist2 != null)
                    {
                        if (statuslist2 != "")
                        {
                            break;
                        }
                    }
                    Thread.Sleep(100);
                }
                IsCSMake = true;
            }
            catch (Exception)
            {
                IsCSMake = true;
                return;
            }
        }
        /// <summary>
        /// 制作冰淇淋
        /// </summary>
        /// <param name="list"></param>
        public static void MakingShows(string sauceCode, string nutletCode)
        {
            try
            {

                IsCSMake = false;
                IOMsg.Instance.AllBol = false;
                IOMsg.Instance.RobotBol = false;
                Thread.Sleep(500);
                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_JBQL_UP);
                if (sauceCode != "0")
                {
                    Thread.Sleep(200);
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_JGJ_UP);
                }
                if (nutletCode != "0")
                {
                    Thread.Sleep(200);
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_JGS_UP);
                }
                while (true)
                {
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_DDBQL);
                    Thread.Sleep(1000);
                    if (IOMsg.Instance.RobotBol)
                    {
                        break;
                    }
                    Thread.Sleep(1000);
                }
                IsIceLC = true;
                Thread.Sleep(100);
                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_JBQL_Down);
                Thread.Sleep(100);
                //IOMsg.Instance.AllBol = true;
                // IceMsg.Instance.AllBol = false;
                Thread.Sleep(200);
                ICSerialPort.Instance.SerialSend(IceMsgbool.Instance.IceChuLiaoStart);
                Thread.Sleep(200);
                while (true)
                {
                    ICSerialPort.Instance.SerialSend(IceMsgbool.Instance.IceChuLiaoOver);
                    Thread.Sleep(200);
                    if (IceMsg.Instance.isover)
                    {
                        break;
                    }
                    Thread.Sleep(100);
                }
                IOMsg.Instance.AllBol = false;
                Thread.Sleep(200);
                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_JBQLWC_UP);
                Thread.Sleep(200);
                //  IOMsg.Instance.AllBol = true;
                //  IceMsg.Instance.AllBol = true;
                if (sauceCode != "")
                {
                    IOMsg.Instance.AllBol = false;
                    IOMsg.Instance.RobotBol = false;
                    Thread.Sleep(200);
                    while (true)
                    {
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_DDGJ);
                        Thread.Sleep(200);
                        if (IOMsg.Instance.RobotBol)
                        {
                            break;
                        }
                        Thread.Sleep(100);
                    }
                    Thread.Sleep(100);
                    if (sauceCode == "1")
                    {
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.GJ1_start);
                        Thread.Sleep(IPorPortInfo.Instance.GJInit * 1000);
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.GJ1_end);
                    }
                    else if (sauceCode == "2")
                    {
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.GJ2_start);
                        Thread.Sleep(IPorPortInfo.Instance.GJInit * 1000);
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.GJ2_end);
                    }
                    else if (sauceCode == "3")
                    {
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.GJ3_start);
                        Thread.Sleep(IPorPortInfo.Instance.GJInit * 1000);
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.GJ3_end);
                    }
                    Thread.Sleep(200);
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_JGJWC_UP);
                    Thread.Sleep(200);
                    // IOMsg.Instance.AllBol = true;
                }
                if (nutletCode != "")
                {
                    IOMsg.Instance.AllBol = false;
                    IOMsg.Instance.RobotBol = false;
                    Thread.Sleep(200);
                    while (true)
                    {
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_DDGS);
                        Thread.Sleep(200);
                        if (IOMsg.Instance.RobotBol)
                        {
                            break;
                        }
                        Thread.Sleep(100);
                    }
                    Thread.Sleep(100);
                    if (nutletCode == "1")
                    {
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.GS_start);
                        Thread.Sleep(IPorPortInfo.Instance.GSInit * 1000);
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.GS_end);
                    }
                    else if (nutletCode == "2")
                    {
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.GR_start);
                        Thread.Sleep(IPorPortInfo.Instance.GSInit * 1000);
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.GR_end);
                    }
                    Thread.Sleep(200);
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_JGSWC_UP);
                    Thread.Sleep(200);
                    //  IOMsg.Instance.AllBol = true;
                }
                IOMsg.Instance.AllBol = false;
                IOMsg.Instance.RobotBol = false;
                Thread.Sleep(200);

                IsCupICE = false;
                IsCSMakeOR = true;
                while (true)
                {
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_JFWC);
                    Thread.Sleep(200);
                    if (IOMsg.Instance.RobotBol)
                    {
                        break;
                    }
                    Thread.Sleep(100);
                }
                Thread.Sleep(200);
                IceMsg.Instance.AllBol = true;
                Clear();

                IsCSMake = true;
            }
            catch (Exception)
            {
                IsCSMake = true;
                return;
            }
        }
        public static void Clear()
        {
            try
            {
                IOMsg.Instance.AllBol = false;
                Thread.Sleep(100);
                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_JBQL_Down);
                Thread.Sleep(100);
                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_JBQLWC_Down);
                Thread.Sleep(100);
                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_JGJ_Down);
                Thread.Sleep(100);
                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_JGJWC_Down);
                Thread.Sleep(100);
                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_JGS_Down);
                Thread.Sleep(100);
                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_JGSWC_Down);
                Thread.Sleep(100);
                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_NC_Down);
                Thread.Sleep(100);
                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_NCWC_Down);
                Thread.Sleep(100);
                IOMsg.Instance.AllBol = true;
            }
            catch (Exception)
            {
                return;
            }
        }
    }
}
